import { Component } from '@angular/core';
import { Http } from '@angular/http';
//Base components
import { OverviewModel } from '../../../shared/templates/overview/ratingOverview.model';
//Services
import { CompanyRiskService } from '../../companyRisk.service';
import { UtilsService } from '../../../shared/services/utils.service';
import { AnalyticsService } from '../../../shared/services/analytics.service'

//Models
import { GoogleAnalyticsBeacon } from '../../../models/googleAnalytics.beacon'

@Component({
    selector: 'company-rating-overview',
    templateUrl: '../../../shared/templates/overview/ratingOverview.component.html',
    styleUrls: ['../../../shared/templates/overview/ratingOverview.component.css'],
    providers: [UtilsService]
})

export class RatingOverviewSummaryComponent extends OverviewModel {    
    constructor(
        private riskService: CompanyRiskService,
        private analyticsService: AnalyticsService,
        private http: Http) {
        super(http);
    }

    ngOnInit() {
        this.loaded = new Promise<void>((resolve, reject) => {
            //clusters
            this.getCompanyClusters().then(clusters => {
                this.riskClusters = clusters;
                this.riskService.company.then(company => {
                    this.lastUpdate = company.lastUpdate;
                    this.currentClusterCode = company.assessmentCode;
                    this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('ESG RR Overview', 'ESG Risk Rating', { CompanyId: company.id }));

                    var cluster = this.riskClusters.filter(function(cluster){
                        return cluster.code == company.assessmentCode;
                    })[0];

                    this.currentClusterName = cluster != undefined ? cluster.name : "N/A";

                    //rating chart
                    this.riskRating = UtilsService.roundUp(company.riskRating);

                    //exposure chart   
                    this.riskFramework = [{
                        name: "Exposure",
                        open: 0,
                        close: company.exposure,
                        labelValue: company.exposure,
                        color: "#B3D4D1"
                    }, 
                    {
                        name: "Managed Risk",
                        open: 0,
                        close: company.managedRisk,
                        labelValue: company.managedRisk,
                        color: "#A7A9AC"
                    },
                    {
                        name: "Overall Unmanaged Risk",
                        open: company.managedRisk,
                        close: company.exposure,
                        labelValue: company.riskRating,
                        color: "#000000"
                    }];                    
                    resolve();
                });
            });
        });
    }
}